/**
 * Created by huachuan on 2016/1/13.
 */
angular.module('homeservice',[])
  //约局的Service
  .factory('Home', function($resource,$rootScope,$cordovaFileTransfer,ENV) {

    //访问的地址
    var APIUrl = ENV.api;
    //接收返回值
    var homeListJson={},
        pageSize=10;
    var homeMemberInfo,memberInfo;
    var collectHomeJson,likeHomeJson,releaseHomeJson,deleteHomeMsgJson,meHomeListJson;
    var newRelease = false,isUploading=false;
    var nowReleaseHome,currentHomeInfo;
    //存储的key
    //获取$resource
    var getHomeMessageListResource = $resource(APIUrl + 'findHome/getHomeMessageList.do');
    var collectHomeResource = $resource(APIUrl + 'findHome/collectHome.do');
    var likeHomeMessageResource = $resource(APIUrl + 'findHome/likeHomeMessage.do');
    var releaseHomeMessageResource = $resource(APIUrl+"releaseHome/releaseHomeMessage.do");
    var deleteHomeByIdResource = $resource(APIUrl + 'findHome/deleteHomeById.do');
    var getMeHomeListResource = $resource(APIUrl + 'meHome/getMeHomeList.do');
    var getCurrentHomeInfoResource = $resource(APIUrl + 'findHome/getCurrentInfo.do');

    return{
      //获取到我的酒窝信息
      getMeHomeList:function(memberId){
        var hasNextPage = true;
        getMeHomeListResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.homeList.length<pageSize){
            hasNextPage = false;
          }
          memberInfo = response.memberInfo;
          meHomeListJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.homeList
          }
          $rootScope.$broadcast('Home.meHomeListUpdated');
        });
      },
      //获取更多我的酒窝信息
      getMoreMeHomeList:function(memberId){
        if(meHomeListJson===undefined){
          return false;
        }
        var hasNextPage=meHomeListJson.hasNextPage;
        var nextPage=meHomeListJson.nextPage;
        var moreHomeData=meHomeListJson.data;
        if(!hasNextPage){
          return false;
        }
        getMeHomeListResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.homeList.length<pageSize){
            hasNextPage = false;
          }
          moreHomeData = moreHomeData.concat(response.homeList);
          meHomeListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreHomeData
          };
          $rootScope.$broadcast('Home.MoreMeHomeListUpdated');
        });
      },
      getMeHomeListJson:function(){
        return meHomeListJson.data;
      },
      //我的酒窝是否有下一页
      meHomeHasNextPage:function(){
        if(meHomeListJson===undefined){
          return false;
        }
        return meHomeListJson.hasNextPage;
      },
      getMemberInfo:function(){
          return memberInfo;
      },
      //保存是否有正在上传的带图片的酒窝
      setUploadingReleaseHome:function(flag){
            isUploading = flag;
      },
      getIsUploadingReleaseHome:function(){
        return isUploading;
      },
      //获取实时的酒窝点赞，评论数量，点赞，与对应的用户的关注状态
      getCurrentHomeInfo:function(searchList){
        getCurrentHomeInfoResource.save({
          search_list:searchList
        },function(response){
          currentHomeInfo = response;
          $rootScope.$broadcast('Home.currentHomeInfoeUpdated');
        });
      },
      getCurrentJson:function(){
        return currentHomeInfo;
      },
      //获取酒窝的信息
      getHomeList:function(memberId){
        var hasNextPage = false;   //是否有下一页
        getHomeMessageListResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.homeMessageDetail.length<pageSize){//判断是否有下一页
            hasNextPage = false;
          }else{
            hasNextPage = true;
          }
          homeListJson = {
            hasNextPage:hasNextPage,
            nextPage: 2,
            data: response.homeMessageDetail
          };
          homeMemberInfo = response.loginmemberInfo;
          $rootScope.$broadcast('Home.homeListResponseUpdated');
        });
      },
      //获取跟更多数据
      getMoreHomeList:function(memberId){
        if(homeListJson===undefined){
          return false;
        }
        var hasNextPage=homeListJson.hasNextPage;
        var nextPage=homeListJson.nextPage;
        var moreHomeData=homeListJson.data;
        if(!hasNextPage){
          return false;
        }
        getHomeMessageListResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.homeMessageDetail.length<pageSize){
            hasNextPage = false;
          }
          moreHomeData = moreHomeData.concat(response.homeMessageDetail);
          homeListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreHomeData
          };
          $rootScope.$broadcast('Home.homeListResponseUpdated');
        });
      },
      //是否有下一页
      hasNextPage: function() {
        if (homeListJson === undefined) {
          return false;
        }
        return homeListJson.hasNextPage;
      },
      getHomeListJson:function(){
        if(homeListJson===undefined){
          return false;
        }
        return homeListJson;
      },
      getHomeMemberInfo:function(){
        return homeMemberInfo;
      },
      //保存发布酒窝的图片----->还未保存的服务器
      saveNewReleaseHome:function(home){
        nowReleaseHome = home;
      },
      getNewReleaseHome:function(){
        return nowReleaseHome;
      },
      //删除酒窝信息
      deleteHome:function(memberId,mhId){
        deleteHomeByIdResource.save({
          member_id:memberId,
          mh_id:mhId
        },function(response){
          deleteHomeMsgJson = response;
          $rootScope.$broadcast('Home.deleteHomeUpdated');
        });
      },
      getDeleteHomeMsgJson:function(){
        return deleteHomeMsgJson;
      },
      //是否有新发布的酒窝信息------------>已经发送成功的
      haveNewRelease:function(){
        return newRelease;
      },
      setNewRelease:function(flag){
        newRelease = flag;
      },
      //发布酒窝
      releaseHomeMessage:function(memberId,homeContent,picSrc,mark,mhLocation){
        releaseHomeMessageResource.save({
          member_id:memberId,
          mh_content:homeContent,
          mh_imgs_url:picSrc,
          mark:mark,
          mh_location:mhLocation
        },function(response){
          releaseHomeJson = response;
          newRelease = true;
          $rootScope.$broadcast('Home.releaseHomeMessageUpdated');
        });
      },
      releaseHomeHavePic:function(memberId,uploadImageList,homeContent,mark,mhLocation){
        var save2DbFileNameList = [];
        var imageList = '';
        var uploadServer = encodeURI(ENV.api+"servlet/imageUploadServlet");
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.mimeType="image/jpeg";
        if(uploadImageList.length==1){//如果是一张图片的时候，传递参数
            options.params = {'from':'home','count':1};
        }
        for(i=0;i<uploadImageList.length;i++){
          options.fileName=uploadImageList[i].substr(uploadImageList[i].lastIndexOf('/')+1);
          $cordovaFileTransfer.upload(uploadServer,uploadImageList[i],options)
            .then(function(result){//上传成功
              if(result.response!='fail'){
                save2DbFileNameList.push(result.response);
                //拼接酒窝图片路径
                if(uploadImageList.length==save2DbFileNameList.length){
                  for(i=0;i<save2DbFileNameList.length;i++){
                    if(i==save2DbFileNameList.length-1){
                      imageList+=save2DbFileNameList[i];
                    }else{
                      imageList+=save2DbFileNameList[i]+";";
                    }
                  }
                  releaseHomeMessageResource.save({
                    member_id:memberId,
                    mh_content:homeContent,
                    mh_imgs_url:imageList,
                    mark:mark,
                    mh_location:mhLocation
                  },function(response){
                    releaseHomeJson = response;
                    newRelease = true;
                    $rootScope.$broadcast('Home.releaseHomePicUpdated');
                  });
                }
              }
            },function(error){//上传失败
              Toast.showMessage("上传失败");
            });
        }

      },
      getReleaseHomeJson:function(){
        return releaseHomeJson;
      },
      //收藏酒窝
      collectHome:function(memberId,mhId){
        collectHomeResource.save({
          member_id:memberId,
          mc_obj_id:mhId
        },function(response){
          collectHomeJson = response;
          $rootScope.$broadcast('Home.collectHomeUpdated');
        });
      },
      getCollectHomeMsgJson:function(){
          return collectHomeJson;
      },
      //点赞酒窝
      likeHome:function(memberId,mhId){
        likeHomeMessageResource.save({
          member_id:memberId,
          mh_id:mhId
        },function(response){
          likeHomeJson = response;
          $rootScope.$broadcast('Home.likeHomeUpdated');
        });
      },
      getLikeHomeMsgJson:function(){
          return likeHomeJson;
      }
    }
  })
//酒窝评论的service
  .factory('HomeComment', function($resource,$rootScope,ENV) {
    var APIUrl = ENV.api;

    var commentListJson,homeCommentJson,deleteCommentJson;

    var homeCommentResource = $resource(APIUrl+"findHome/getHomeCommentList.do");
    var publishCommentResource = $resource(APIUrl+"findHome/publishComment.do");
    var deleteCommentResource =  $resource(APIUrl+"findHome/delMHomeComment.do");
    //获取的约局的评论列表
    return {
      //删除评论
      deleteComment:function(mhcId,memberId,mhId){
        deleteCommentResource.save({
          mhc_id:mhcId,
          member_id:memberId,
          mh_id:mhId
        },function(response){
          deleteCommentJson = response;
          $rootScope.$broadcast('HomeComment.deleteCommentUpdated');
        });
      },
      getDelCommentJson:function(){
        return deleteCommentJson;
      },
      getCommentList:function(mhId,memberId){
        homeCommentResource.save({
          mh_id:mhId,
          member_id:memberId
        },function(response){
          commentListJson = response;
          $rootScope.$broadcast('Home.CommentListUpdated');
        })
      },
      //发表评论
      publishComment:function(mhId,memberId,newComment){
        publishCommentResource.save({
          member_id:memberId,
          mh_id:mhId,
          mhc_content:newComment
        },function(response){
          homeCommentJson = response;
          $rootScope.$broadcast('Home.publishCommentUpdated');
        });
      },
      getCommentJson:function(){
        return commentListJson;
      },
      //获取到新增的评论内容
      getNewComment:function(){
        return homeCommentJson;
      }
    }
  })
;

;
